/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionengine.gameserver.model.gameobjects;

import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.house.House;
import com.aionengine.gameserver.model.items.storage.StorageType;
import com.aionengine.gameserver.model.templates.housing.HousingStorage;
import com.aionengine.gameserver.network.aion.serverpackets.SM_OBJECT_USE_UPDATE;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.network.aion.serverpackets.SM_WAREHOUSE_INFO;
import com.aionengine.gameserver.utils.PacketSendUtility;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Rolandas
 */
public final class StorageObject extends HouseObject<HousingStorage> {

    private AtomicReference<Player> usingPlayer = new AtomicReference<Player>();

    public StorageObject(House owner, int objId, int templateId) {
        super(owner, objId, templateId);
    }

    @Override
    public void onUse(Player player) {
        if (player.getObjectId() != this.getOwnerHouse().getOwnerId()) {
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_HOUSING_OBJECT_IS_ONLY_FOR_OWNER_VALID);
            return;
        }
        if (!usingPlayer.compareAndSet(null, player)) {
            return;
        }
        try {
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_HOUSING_OBJECT_USE(getObjectTemplate().getNameId()));
            PacketSendUtility.sendPacket(player, new SM_OBJECT_USE_UPDATE(player.getObjectId(), 0, 0, this));

            for (HouseObject<?> ho : getOwnerHouse().getRegistry().getSpawnedObjects()) {
                if (ho instanceof StorageObject) {
                    int warehouseId = ((HousingStorage) ho.getObjectTemplate()).getWarehouseId() + StorageType.HOUSE_WH_MIN - 1;
                    PacketSendUtility.sendPacket(player, new SM_WAREHOUSE_INFO(player.getStorage(warehouseId).getItemsWithKinah(), warehouseId, 0,
                            true, player));
                    PacketSendUtility.sendPacket(player, new SM_WAREHOUSE_INFO(null, warehouseId, 0, false, player));
                }
            }
        } finally {
            usingPlayer.set(null);
        }
    }

    @Override
    public boolean canExpireNow() {
        return usingPlayer.get() == null;
    }
}
